package io.summer.redis.tool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Set;

/**
 * 提供对RedisTemplate-cli的映射
 *
 * @author Moonlight
 */
public final class RedisTool {
  private RedisTemplate<Object, Object> redisTemplate;

  public RedisTemplate<Object, Object> template() {
    return redisTemplate;
  }

  private StringRedisTemplate stringRedisTemplate;

  public StringRedisTemplate stringTemplate() {
    return stringRedisTemplate;
  }

  /**
   * no @Component, autowired work fine!
   * 静态注入与ConditionOnBean结合不是很好
   */
  @Autowired
  private void setStringRedisTemplate(StringRedisTemplate template) {
    stringRedisTemplate = template;
  }

  @Autowired
  private void setRedisTemplate(RedisTemplate<Object, Object> template) {
    redisTemplate = template;
  }

  public <HK, HV> HashOperations<String, HK, HV> hash() {
    return stringRedisTemplate.opsForHash();
  }

  public ListOperations<String, String> list() {
    return stringRedisTemplate.opsForList();
  }

  public HashOperations<Object, Object, Object> oHash() {
    return redisTemplate.opsForHash();
  }

  public ListOperations<Object, Object> oList() {
    return redisTemplate.opsForList();
  }

  public Set<String> keys(String pattern) {
    return stringRedisTemplate.keys(pattern);
  }
}
